package com.mall.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.mall.common.utils.BeanUtils;
import com.mall.common.utils.UserContext;
import com.mall.domain.dto.AddressDTO2;
import com.mall.domain.po.Address;
import com.mall.domain.response.Response;
import com.mall.mapper.AddressMapper;
import com.mall.service.IAddressService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 */
@Service
public class AddressServiceImpl extends ServiceImpl<AddressMapper, Address> implements IAddressService {

    @Override
    public Response<List<AddressDTO2>> getAddress() {

        Long userId = UserContext.getUser();
        QueryWrapper<Address> addressQueryWrapper = new QueryWrapper<>();
        addressQueryWrapper.eq("user_id", userId);

        List<Address> addressList = this.list(addressQueryWrapper);
        List<AddressDTO2> addressDTO2s = new ArrayList<>();

        for (Address address : addressList) {
            AddressDTO2 addressDTO2 = new AddressDTO2();
            BeanUtils.copyProperties(address, addressDTO2);
            addressDTO2s.add(addressDTO2);
        }
        return Response.success(addressDTO2s);
    }

    @Override
    public Response<?> addAddress(String province, String city, String town, String street, String username, String mobile) {

        if(province==null||city==null||town==null||street==null||username==null||mobile==null){
            return Response.fail("输入不能为空");
        }
        Long userId = UserContext.getUser();
        Address address = new Address();
        address.setProvince(province).setCity(city).setTown(town).setStreet(street).setContact(username).setMobile(mobile).setIsDefault(0).setUserId(userId);
        save(address);
        return Response.success("新增成功");
    }

    @Override
    @Transactional
    public Response<?> deleteAddress(ArrayList<Long> ids) {

        removeBatchByIds(ids);
        return Response.success("删除成功");
    }

    @Override
    public Response<?> editAddress(Address address) {

        updateById(address);
        return Response.success("修改成功");
    }

    @Override
    public Response<?> is_default(Long id) {

        QueryWrapper<Address> addressQueryWrapper = new QueryWrapper<>();
        addressQueryWrapper.eq("is_default",1);
        Address address=this.getOne(addressQueryWrapper);
        address.setIsDefault(0);

        Address address1=getById(id);
        address1.setIsDefault(1);
        updateById(address);
        updateById(address1);
        return Response.success("修改成功");
    }

    @Override
    public Response<Address> defaultAddress() {

        Long userId = UserContext.getUser();
        QueryWrapper<Address> addressQueryWrapper = new QueryWrapper<>();
        addressQueryWrapper.eq("user_id",userId).eq("is_default",1);
        Address one = getOne(addressQueryWrapper);
        return Response.success(one);
    }
}
